home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / wc.c < prev    next >
Text File  |  1985-11-13  |  1KB  |  41 lines

  1.  
  2. /*  This program counts words in an ASCII file. Les Hancock
  3. is the author.   */
  4.  
  5. #define NO 0
  6. #define YES 1
  7. #define TAB 9
  8. #define LF 10
  9. #define CR 13
  10. #define EOF 26
  11. main (argc, argv)
  12. int argc;
  13. char *argv[];
  14. {
  15.      int fid;
  16.      unsigned linect, wordct, cct;
  17.      char buf[134], c, inword;
  18.      if ((fid = fopen (argv[1], buf)) == -1)  {
  19.         printf ("File missing.\nUsage: WC <filename.ext>\n");
  20.         exit ();
  21.     }
  22.     for (linect = wordct = cct = 0, inword = NO;
  23.          (c = getc(buf)) != EOF; ++cct) {
  24.       if (c > 7 && (c < 14 || c > 31) && c < 128)  {
  25.          if (c == CR)
  26.            ++linect;
  27.       if (c == CR || c == LF || c == ' ' || c == TAB)
  28.           inword = NO;
  29.       else if (!inword)  {
  30.          inword = YES;
  31.          ++wordct;
  32.          }
  33.      } else {
  34.         printf ("Not ASCII file\n");
  35.         exit ();
  36.      }
  37.   }
  38.   printf ("Lines = %u\nWords = %u\nCharacters = %u\n", linect, wordct, cct);
  39.   close(fid);
  40. }
  41.